package com.ship.common.business.config;

import com.baomidou.mybatisplus.core.handlers.MetaObjectHandler;
import com.ship.common.security.utils.SecurityUtils;
import org.apache.commons.lang3.StringUtils;
import org.apache.ibatis.reflection.MetaObject;

import java.util.Date;


/**
 * @author 31463
 */
public class DateMetaObjectHandler implements MetaObjectHandler {

    /**
     * 更新时间字段名
     */
    private final String createDateField = "createDate";
    /**
     * 修改人字段名
     */
    private final String createByField = "createBy";


    /**
     * 插入填充，字段为空自动填充
     */
    @Override
    public void insertFill(MetaObject metaObject) {
        Object createDate = getFieldValByName(createDateField, metaObject);
        if(createDate == null){
            setFieldValByName(createDateField, new Date(), metaObject);
        }
        Object createBy = getFieldValByName(createByField, metaObject);
        if(createBy == null || StringUtils.isBlank(createBy.toString())){
            setFieldValByName(createByField, SecurityUtils.getUsername(), metaObject);
        }
    }

    /**
     * 更新填充
     */
    @Override
    public void updateFill(MetaObject metaObject) {

    }
}
